
Ext.ux.Login = function(config) {

	var login = new Ext.FormPanel({
			labelWidth: 65
		,	url: Config.baseUrl + 'resources/api/app.php'
		,	baseParams: {
				cmd: 'sign_in'
			}
		,	bodyStyle: 'padding: 10px'
		,	border: false
		,	defaultType: 'textfield'
		,	monitorValid: true
		,	items: [{
					xtype: "component"
				,	autoEl: {
						"html": String.format('Welcome to the Members section of the {0} working group.<br/> If you require a login, or have forgotten yours, please <a href=\'mailto:{1}\'>contact us</a>.<br/><br/>', Config.mainTitle, Config.contactEmail)
					}
			}, { 
					fieldLabel: 'Username'
				,	name: 'username'
				,	id: 'username'
				,	allowBlank: false 
				,	width: 180
			}, {
					fieldLabel: 'Password'
				,	name: 'password' 
				,	inputType: 'password'
				,	allowBlank: false 
				,	width: 180
				,	enableKeyEvents: true
				,	listeners: {
						keypress: function( textfield, e ) {
							if (e.getKey() ==  e.ENTER) {
								login.ownerCt.signin();
							}
						}
					}
			}]
	});
	
	Ext.apply(this, config, { 
			width: 285
		,	height: 200
		,	id: 'login'
		,	closeAction: 'hide'
		,	title: Config.loginTitle
		,	layout: 'fit'
		,	border: false
		,	closable: false
		,	draggable: false
		,	resizable: false
		, 	defaultButton: 'username'		
		,	modal: true
		,	buttons: [{ 
					text: 'Sign in'
				,	id: 'btn-signin'
				,	scope: this
				,	handler: this.signin
			}] 				
		,	items: [ login ]
	});

	Ext.ux.Login.superclass.constructor.apply(this, arguments);

};

Ext.extend( Ext.ux.Login, Ext.Window, {			 
	signin: function() {
		var login = this.items.first();		
		Ext.getCmp('btn-signin').disable();
		this.name = login.getForm().findField('username').getValue();	
		login.getForm().submit({ 
				method: 'POST'
			,	scope: this
			,	success: function( b, r ) {
					login.enable();
					Ext.getCmp('btn-signin').enable();
					this.data = Ext.decode(r.response.responseText);	
					if ( this.data.success ) {
						this.fireEvent('login_success', this.data, this);
					}
				}
			,	failure: function(form, action) {
					if (action.failureType == 'server') { 
						obj = Ext.util.JSON.decode(action.response.responseText); 
						Ext.Msg.alert('Login Failed!', obj.error.msg); 
					} else { 
							Ext.Msg.alert('Warning!', 'Authentication Failed: Username/Password was not correct.'); 
					} 
					login.enable();
					Ext.getCmp('btn-signin').enable();
					login.getForm().reset(); 
					Ext.get('username').focus(false, 200);
				} 
			}); 
						
	}
});